using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SyncUdp.Model
{
    using SyncUdp.CommonDefined;
    using SyncUdp.Event.GameEvent;
    using SyncUdp.Event.NetworkEvent;
    using SyncUdp.Interface;
    using SyncUdp.MethodExtension;
    using System;

    public interface IPlayerModel : ISingleton
    {
        /// <summary>
        /// 玩家的数据
        /// </summary>
        public PlayerData Data { get; }

        public PlayerInputs Inputs { get; }

        public void SetInputs(bool sprinting, Vector2 movementInput);//, Vector2 mouseMovementInput);
    }
    
    /// <summary>
    /// 玩家的数据
    /// </summary>
    public class PlayerModel : AbstractSingleton, IPlayerModel
    {
        IGameManager _gm;
        PlayerData _data;
        PlayerInputs _inputs;

        void OnSOLoaded(PlayerDataSettingSO so)
        {
            _data = new PlayerData()
            {
                walkSpeed = so.walkSpeed,
                runSpeedMultiplier = so.runSpeedMultiplier,
                turnSpeed = so.turnSpeed,
                turnSpeedMultiplier = so.turnSpeedMultiplier,
            };
        }

        #region 方法实现
        public PlayerData Data { get { return _data; } }

        public PlayerInputs Inputs { get { return _inputs; } }

        protected override void OnInit()
        {
            var resMgr = this.GetInstance<IResourceManager>();
            _gm = this.GetInstance<IGameManager>();
            resMgr.LoadAsync<PlayerDataSettingSO>(_gm.AllPaths.playerDataSettingSOPath, OnSOLoaded);
            _inputs = new PlayerInputs();
        }

        public void SetInputs(bool sprinting, Vector2 movementInput)//, Vector2 mouseMovementInput)
        {
            _inputs.isSprinting = sprinting;
            // 浮点数保留两位小数，并转化为整数，方便通信，减小误差
            float units = _gm.CommonData.unitsMultiplier;
            _inputs.movementInput.x = (int)(movementInput.x * units);
            _inputs.movementInput.y = (int)(movementInput.y * units);
            // 目前还不需要鼠标输入，不仅是因为玩家旋转不会跟随鼠标移动；
            // 而且没有要求制作观战功能，虽然以后可能会做。
            //_inputs.mouseMovementInput.x = (int)(movementInput.x * units);
            //_inputs.mouseMovementInput.y = (int)(movementInput.y * units);
            SendEvent(new PlayerModelReceiveInputsEvent() { inputs = _inputs });
        }

        #endregion
    }

    /// <summary>
    /// 玩家的数据
    /// </summary>
    public class PlayerData
    {
        public float walkSpeed;

        public float runSpeedMultiplier;

        public float turnSpeed;

        public float turnSpeedMultiplier;
    }

    [Serializable]
    public class PlayerInputs
    {
        public bool isSprinting;
        public Vector2Int movementInput;

        /// <summary>
        /// 判断是否输入了
        /// </summary>
        /// <returns></returns>
        public bool CheckInvaildInput()
        {
            return isSprinting == false && movementInput == Vector2Int.zero;
        }
    }
}
